package i.i.myapplication;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import java.util.ArrayList;
import java.util.List;


//import i.i.myapplication.model.Music;
import i.i.myapplication.controller.MusicAdapter;
import i.i.myapplication.model.Music;
import i.i.myapplication.view.MusicDetailActivity;
import i.i.myapplication.view.MusicService;


//package i.i.myapplication;


public class MusicFragment extends Fragment {

    private ListView listView;
    private MusicAdapter adapter;
    private List<Music> musicList;

    private MusicService.MusicControl musicControl;  // 绑定Service后拿到的控制对象
    private ServiceConnection serviceConnection;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_music, container, false);
        listView = rootView.findViewById(R.id.lv);

        initMusicList();

        adapter = new MusicAdapter(requireContext(), musicList, new MusicAdapter.OnPlayClickListener() {
            @Override
            public void onPlayClick(int position) {
                if (musicControl != null) {
                    musicControl.play(position + 1); // 假设音乐文件命名为 music1, music2 ...
                }
            }
        });
        listView.setAdapter(adapter);


        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Music music = musicList.get(position);
                Intent intent = new Intent(requireContext(), MusicDetailActivity.class);
                // 传递歌曲信息
                intent.putExtra("song_name", music.getName());
                intent.putExtra("artist", music.getArtist());
                intent.putExtra("image_res", music.getImageResId());
                intent.putExtra("music_res", music.getMusicResId());
                intent.putExtra("position", position);  // 传当前索引方便上一首下一首
                startActivity(intent);
            }
        });


        // 绑定MusicService，获取控制对象
        Intent intent = new Intent(requireContext(), MusicService.class);
        requireActivity().bindService(intent, getServiceConnection(), Context.BIND_AUTO_CREATE);

        return rootView;
    }

    private void initMusicList() {
        musicList = new ArrayList<>();

        musicList.add(new Music(R.drawable.img_01, "Innocence-A R L 1223", "艾薇儿sdsdsd", R.raw.music1));
        musicList.add(new Music(R.drawable.img_02, "Love Story", "Taylor Swift", R.raw.music2));
        musicList.add(new Music(R.drawable.img_03, "不用去猜-Jony J", "Jony J", R.raw.music3));
        musicList.add(new Music(R.drawable.img_04, "一人有一个梦想-黎瑞恩", "黎瑞恩", R.raw.music3));
        // 根据你资源添加更多...
    }

    private ServiceConnection getServiceConnection() {
        if (serviceConnection == null) {
            serviceConnection = new ServiceConnection() {
                @Override
                public void onServiceConnected(ComponentName name, IBinder service) {
                    musicControl = (MusicService.MusicControl) service;
                }

                @Override
                public void onServiceDisconnected(ComponentName name) {
                    musicControl = null;
                }
            };
        }
        return serviceConnection;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (serviceConnection != null) {
            requireActivity().unbindService(serviceConnection);
            serviceConnection = null;
        }
    }
}
